/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"errors"
	"fmt"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
	. "saas_sys_base/modules/appmanager/models"
)

type AppSuitService struct {
	// add your model here
	tbAppSuit AppSuit
	tbAppSuitItem AppSuitItem
}

func (t *AppSuitService) ListAppSuit(keyword string, page, psize int, orderField, order string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_app_suit", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}

	return lego.QueryListByMap(queryMap)
}

func (t *AppSuitService) ListAppSuitByWho(who *common.Who, keyword string, page, psize int, orderField, order string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_app_suit", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			//map[string]string{"table": "tb_xxx as t2", "on": "t2.xxx = t1.xxx"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			//{"xx.tenant_id", "=", who.TenantId, "AND", false},
			//{"xx.mini_app_id", "=", who.MiniAppId, "AND", false},

		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}
	/*{ //这段生成代码的目的是，当需要根据条件判断，来增加对应where条件时，使用这里的逻辑
		queryWhere := queryMap["where"].([]lego.QMapWhereField)
		if who.TenantId != 0 {
			queryWhere = append(queryWhere, lego.QMapWhereField{"t2.tenant_id", "=", who.TenantId, "AND", false})
		}

		if who.MiniAppId != 0 {
			queryWhere = append(queryWhere, lego.QMapWhereField{"t2.mini_app_id", "=", who.MiniAppId, "AND", false})
		}

	}*/

	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *AppSuitService) GetAppSuitById(id int64) *AppSuit {
	return t.tbAppSuit.RecodeById(id)
}
/* 根据id获取记录
 * -----------------
 */
func (t *AppSuitService) GetAppSuitBySuitCode(id int64) *AppSuit {
	return t.tbAppSuit.RecodeBySuitCode(id)
}
/* 添加
 * -----------------
 */
func (t *AppSuitService) SaveAppSuit(item2add *AppSuit) bool {
	return t.tbAppSuit.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *AppSuitService) DelAppSuitById(id int64) bool {
	return t.tbAppSuit.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *AppSuitService) UpdateAppSuit(rc *AppSuit) bool {
	return t.tbAppSuit.UpdateRecode(rc)
}


func (t *AppSuitService)AddAppSuit(rc *AppSuit) error {
	// 业务处理
	recode := t.GetAppSuitBySuitCode(rc.AppSuitCode)
	if recode == nil {
		// 新建
		s := lego.NewDBSession()
		s.Begin()
		if false == t.tbAppSuit.AddRecodeWithsession(s.GetSession(), rc) {
			s.Rollback()
			errors.New("AddAppSuit tbAppSuit.AddRecodeWithsession failed")
		}

		for _, v := range rc.Applist{
			appitem := new(AppSuitItem)
			appitem.MiniAppId = v.MiniAppId
			appitem.CheckMode = v.CheckMode

			appitem.AppSuitId = rc.Id  //将上面的AppSuit表项id赋值在这里
			appitem.AppSuitCode = rc.AppSuitCode

			if false == t.tbAppSuitItem.AddRecodeWithsession(s.GetSession(), appitem) {
				errors.New("AddAppSuit tbAppSuitItem.AddRecodeWithsession failed")
			}
		}
		s.Commit()
	}

	return nil
}


func (t *AppSuitService) List(keyword string, page, psize int) ([]AppSuit, int64, error) {
	return t.tbAppSuit.List(keyword, page, psize)
}